package com.management.phonemall.controller;


import com.management.phonemall.dto.UserLoginParam;
import com.management.phonemall.entity.Custom;
import com.management.phonemall.entity.Staff;
import com.management.phonemall.repository.StaffRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


@Controller
public class DashBoardController {

    Logger logger = LoggerFactory.getLogger(DashBoardController.class);

    @Autowired
    StaffRepository staffRepository;

    private ModelAndView modelAndView = new ModelAndView();

    @RequestMapping(value = "/", method = RequestMethod.POST)
    public ModelAndView dashboard(HttpServletRequest request, HttpServletResponse response, UserLoginParam user) {
        HttpSession session = request.getSession();
        //ModelAndView modelAndView = new ModelAndView();
        logger.info(user.toString());
        String verifyCode = (String) session.getAttribute("verifyCode");
        Staff currentUser = staffRepository.getStaffByNameAndPassword(user.getName(), user.getPassword());
        if (currentUser != null && verifyCode.equals(user.getVerifycode())) {
            modelAndView.setViewName("dashboard/main");
            modelAndView.addObject("user", currentUser);
            modelAndView.addObject("body", new String("body"));
            return modelAndView;
        } else {
            request.getSession().invalidate();
            modelAndView.setViewName("redirect:/login");
        }
        return null;
    }


}
